package com.easylinkin.linkappapi.safe.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Date;
import java.util.Optional;

import com.easylinkin.linkappapi.common.exceptions.BusinessException;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.quality.dto.QualityActionRecordsDTO;
import com.easylinkin.linkappapi.quality.entity.QualityActionRecords;
import com.easylinkin.linkappapi.safe.dao.HiddenActionRecordsMapper;
import com.easylinkin.linkappapi.safe.dto.HiddenActionRecordsDTO;
import com.easylinkin.linkappapi.safe.entity.HiddenActionRecords;
import com.easylinkin.linkappapi.safe.service.HiddenActionRecordsService;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 安全管理查阅记录表 服务实现类
 * </p>
 *
 * @author TongJie
 * @since 2022-04-13
 */
@Service
public class HiddenActionRecordsServiceImpl extends ServiceImpl<HiddenActionRecordsMapper, HiddenActionRecords> implements HiddenActionRecordsService {

  @Autowired
  private LinkappUserContextProducer linkappUserContextProducer;

  /**
   * 增加
   * @param hiddenActionRecords
   */
  @Override
  public void insert(HiddenActionRecords hiddenActionRecords) {
    /**
     * 验证重复
     */
//    this.checkExist(hiddenActionRecords);
    //设置基本属性
    this.setBase(hiddenActionRecords);
    this.save(hiddenActionRecords);
  }

  /**
   * @Description: 根据id查询安全管理查阅记录详情
   * @author kan yuanfeng
   * @date 2023/04/04 11:42
   */
  @Override
  public HiddenActionRecordsDTO findById(Integer id) {
    RequestModel<HiddenActionRecords> requestModel = new RequestModel<>();
    requestModel.setPage(new Page(1,1));
    HiddenActionRecords hiddenActionRecords = new HiddenActionRecords();
    hiddenActionRecords.setId(id);
    requestModel.setCustomQueryParams(hiddenActionRecords);
    IPage<HiddenActionRecordsDTO> page = this.queryListByPage(requestModel);
    if (CollectionUtil.isNotEmpty(page.getRecords())){
      return page.getRecords().get(0);
    }
    return null;
  }

  @Override
  public IPage<HiddenActionRecordsDTO> queryListByPage(RequestModel<HiddenActionRecords> requestModel) {
    Page page = requestModel.getPage();
    HiddenActionRecords hiddenActionRecords = requestModel.getCustomQueryParams();
    IPage<HiddenActionRecordsDTO> hiddenActionRecordsIPage = baseMapper.queryListByPage(page, hiddenActionRecords);
    return hiddenActionRecordsIPage;
  }

  /**
   * 验证重复
   */
  private void checkExist(HiddenActionRecords hiddenActionRecords) {
    QueryWrapper<HiddenActionRecords> queryWrapper = new QueryWrapper<>();
    //todo 设置判断重复条件
    //编辑的时候存在id
    Optional.ofNullable(hiddenActionRecords.getId()).ifPresent(id -> queryWrapper.ne("id_",hiddenActionRecords.getId()));
    Integer integer = baseMapper.selectCount(queryWrapper);
    if (integer>0){
      throw new BusinessException("该安全管理查阅记录已存在");
    }
  }

  /**
   * 设置基本属性
   * @param hiddenActionRecords
   */
  private void setBase(HiddenActionRecords hiddenActionRecords) {
    hiddenActionRecords.setModifyTime(new Date());
    if(null != linkappUserContextProducer.getCurrent()){
      hiddenActionRecords.setModifyId(linkappUserContextProducer.getCurrent().getId());
    }
    //没有id就是新增,有就是编辑
    if (null == hiddenActionRecords.getId()){
      if(null != linkappUserContextProducer.getCurrent()){
        hiddenActionRecords.setCreatorId(linkappUserContextProducer.getCurrent().getId());
      }
      hiddenActionRecords.setCreateTime(new Date());
      if(StringUtils.isBlank(hiddenActionRecords.getTenantId())){
        hiddenActionRecords.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
      }
    }
  }
}
